package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.common.utils.SecurityUtils;
import com.lechi.web.service.IIotDeviceSensorRelService;
import com.lechi.web.service.IIotSensorService;
import com.lechi.web.system.domain.IotDeviceSensorBo;
import com.lechi.web.system.domain.IotDeviceSensorRelBo;
import com.lechi.web.system.domain.IotSensorBo;
import com.lechi.web.vo.IotSensorQueryVo;
import com.lechi.web.vo.IotSensorVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 设备观测要素Controller
 *
 * @author yangguang
 */
@RestController
@Validated
@RequestMapping("/iot/sensor")
@RequiredArgsConstructor
public class IotSensorController extends BaseController {

    private final IIotSensorService iotSensorService;


    private final IIotDeviceSensorRelService iotDeviceSensorRelService;


    /**
     * 查询设备观测要素列表
     *
     * @param iotSensorVo the iot sensor vo
     * @return the table data info
     */
    @GetMapping("/list")
    //    @PreAuthorize("@ss.hasPermi('iot:sensor:query')")
    public TableDataInfo list(IotSensorQueryVo iotSensorVo) {
        startPage();
        IotSensorBo iotSensorBo = new IotSensorBo();
        BeanUtils.copyProperties(iotSensorVo, iotSensorBo);
        List<IotSensorBo> retList = iotSensorService.selectIotSensorList(iotSensorBo);
        return getDataTable(retList);
    }

    /**
     * 获取设备观测要素详细信息
     *
     * @param id the id
     * @return the info
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iotSensorService.selectIotSensorById(new Long[]{id}));
    }

    /**
     * 新增设备观测要素
     *
     * @param iotSensorVo the iot sensor vo
     * @return the ajax result
     */
    @PostMapping("/add")
    @Log(title = "新增设备观测要素", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Valid IotSensorVo iotSensorVo) {
        IotSensorBo iotSensorBo = new IotSensorBo();
        BeanUtils.copyProperties(iotSensorVo, iotSensorBo);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotSensorBo.setCreateBy(username);
        iotSensorService.insertIotSensor(iotSensorBo);
        return AjaxResult.success();
    }

    /**
     * 修改设备观测要素
     *
     * @param iotSensorVo the iot sensor vo
     * @return the ajax result
     */
    @PostMapping("/edit")
    @Log(title = "修改设备观测要素", businessType = BusinessType.UPDATE)
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.SERIALIZABLE, timeout = 60)
    public AjaxResult edit(@RequestBody @Valid IotSensorVo iotSensorVo) {
        IotSensorBo iotSensorBo = new IotSensorBo();
        BeanUtils.copyProperties(iotSensorVo, iotSensorBo);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotSensorBo.setUpdateBy(username);
        iotSensorService.updateIotSensor(iotSensorBo);
        return AjaxResult.success();
    }

    /**
     * 删除设备观测要素
     *
     * @param ids the ids
     * @return the ajax result
     */
    @DeleteMapping("/{ids}")
    @Log(title = "删除设备观测要素", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        IotDeviceSensorRelBo iotDeviceGroupRel = new IotDeviceSensorRelBo();
        Arrays.asList(ids).forEach(id -> {
            iotDeviceGroupRel.setSensorId(id);
            List<IotDeviceSensorBo> ret = iotDeviceSensorRelService.selectIotDeviceSensorRelList(iotDeviceGroupRel);
            if (ret != null && !ret.isEmpty()) {
                throw new ServiceException(ErrorCodeEnum.DELETE_SENSOR_ERROR.getValue(), ErrorCodeEnum.DELETE_SENSOR_ERROR.getKey());
            }
            iotSensorService.deleteIotSensorByIds(ids);
        });
        return AjaxResult.success();
    }
}
